use std::{
    net::IpAddr,
    sync::{Arc, Mutex},
};

use clap::Parser;

#[derive(Clone)]
pub(crate) struct AppState {
    pub filename: Arc<String>,
    pub count: Arc<Mutex<u8>>,
    pub upload: bool,
}

/// 一个基于局域网的跨平台分享软件
/// 使用方法如下：
#[derive(Debug, Parser)]
#[command(author, version, about, long_about)]
pub(crate) struct Args {
    /// 可选的IP地址
    #[arg(long, short)]
    pub ip: Option<IpAddr>,
    /// 可选的端口号
    #[arg(short, long)]
    pub port: Option<u16>,
    #[arg(short, long)]
    pub count: Option<u8>,
    /// 启用上传模式
    #[arg(short, long)]
    pub upload: bool,
    /// 需要分享的文件名
    #[arg(required_unless_present = "upload")]
    pub filename: Option<String>,
}

impl AppState {
    pub(crate) fn new<T: AsRef<str>>(filename: T, count: u8, upload: bool) -> Self {
        Self {
            filename: Arc::new(filename.as_ref().to_owned()),
            count: Arc::new(Mutex::new(count)),
            upload,
        }
    }
}
